﻿using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace zaklad_rtv
{
    public abstract class AbstractList<T> : UserControl, IFilterable where T : IDatabaseObject
    {
        private Collection<T> _objectCollection;

        public MainWindow MainWindow { get; set; }
        public Collection<T> ObjectCollection
        {
            get { return _objectCollection; }
            set
            {
                _objectCollection = value;
                RefreshList();
            }
        }

        public event DatabaseObjectChangedEventHandler DatabaseObjectChanged
        {
            add { AddHandler(AppEvents.DatabaseObjectChangedEvent, value); }
            remove { RemoveHandler(AppEvents.DatabaseObjectChangedEvent, value); }
        }

        public void Filter()
        {
            var criteria = ComposeCriteria();
            ViewFilteredList(criteria);
        }

        protected abstract void RefreshList();
        protected abstract ICriteria<T> ComposeCriteria();
        protected abstract void ViewFilteredList(ICriteria<T> criteria);
    }
}
